import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;

public class TableHeadTest
{
	public static void main(String[] args)
	{
		new TableHeadTest().dynamicHeadWrite();
	}

	public void dynamicHeadWrite()
	{
		  // 头的策略
	    WriteCellStyle headWriteCellStyle = new WriteCellStyle();
	    
	    WriteFont headWriteFont = new WriteFont();
	    headWriteFont.setColor(IndexedColors.BLACK.getIndex());
	    headWriteFont.setBold(true);
	    headWriteFont.setFontName("宋体");
	    //headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
	    headWriteCellStyle.setWriteFont(headWriteFont);
	    headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
	    headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//	    headWriteCellStyle.setBottomBorderColor(IndexedColors.RED.getIndex());
	    headWriteCellStyle.setBorderLeft(BorderStyle.THIN);
	    headWriteCellStyle.setBorderRight(BorderStyle.THIN);
	    headWriteCellStyle.setBorderTop(BorderStyle.THIN);
	    headWriteCellStyle.setBorderBottom(BorderStyle.THIN);
	    headWriteCellStyle.setFillBackgroundColor(IndexedColors.BLACK.index);
	    headWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);
	    headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
	    
	    // 内容的策略
	    WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
	    contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
	    contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
	    contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
	    contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
	    contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
	    contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
	    WriteFont contentWriteFont = new WriteFont();
	    contentWriteFont.setColor(IndexedColors.BLACK.getIndex());
	    contentWriteFont.setBold(false);
	    contentWriteFont.setFontName("宋体");
	    contentWriteCellStyle.setWriteFont(contentWriteFont);
	    
	    
	    // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
	    HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
		
	    
	    OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy2 = new OnceAbsoluteMergeStrategy(4, 5, 3, 5);//0，1表示第1行到第2行 1,2表示第2列到第3列
		String fileName = "dynamicHeadWrite" + System.currentTimeMillis() + ".xlsx";
//		EasyExcel.write(fileName)
//				.registerWriteHandler(horizontalCellStyleStrategy)
//				.registerWriteHandler(onceAbsoluteMergeStrategy2)
//				.useDefaultStyle(false).sheet("模板").head(head()).doWrite(data());
		
		
		List<WriteHandler> listw=Arrays.asList(horizontalCellStyleStrategy,onceAbsoluteMergeStrategy2);
		
		ExcelWriterBuilder excelWriterBuild = EasyExcel.write(fileName);
		ExcelWriter excelWriter=excelWriterBuild.build();
        for (int i = 0; i < 5; i++) {
            // 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样
            WriteSheet writeSheet = EasyExcel.writerSheet(i, "模板" + i).build();
            if(i==0) {
            	 writeSheet.setCustomWriteHandlerList(listw);
            }
            writeSheet.setHead(head());
            excelWriter.write(data(), writeSheet);
        }
        excelWriter.close();
		
	}

	private List<List<Object>> data()
	{
		List<List<Object>> list = new ArrayList<List<Object>>();
		List<Object> list0 = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
		List<Object> list00 = Arrays.asList(11, 21, 31, 41, 51, 61, 71, 81, 91, 101);
		List<Object> list1 = Arrays.asList("11", "22", "33", "44", "55", "66", "77", "88", "99", "1010");

		list.add(list0);
		list.add(list00);
		list.add(list1);
		return list;
	}

	private List<List<String>> head()
	{
		List<TableHeader> list = new ArrayList<>();

		// 目录1
		TableHeader t1 = new TableHeader("目录1");
		List<TableHeader> children = new ArrayList<>();
		TableHeader t11 = new TableHeader("1-1");
		children.add(t11);
		List<TableHeader> t11children = new ArrayList<>();
		t11children.add(new TableHeader("1-1-1"));
		t11children.add(new TableHeader("1-1-2"));
		t11.setChildren(t11children);

		children.add(new TableHeader("1-2"));
		children.add(new TableHeader("1-3"));
		t1.setChildren(children);
		list.add(t1);

		// 目录2
		TableHeader t2 = new TableHeader("目录2");
		List<TableHeader> t2children = new ArrayList<>();
		t2.setChildren(t2children);
		t2children.add(new TableHeader("2-1"));

		TableHeader t22 = new TableHeader("2-2");
		List<TableHeader> t22children = new ArrayList<>();
		t22.setChildren(t22children);
		t22children.add(new TableHeader("2-2-1"));
		t22children.add(new TableHeader("2-2-2"));
		TableHeader t223 = new TableHeader("2-2-3");
		List<TableHeader> t223children = new ArrayList<>();
		t223children.add(new TableHeader("2-2-3-1"));
		t223children.add(new TableHeader("2-2-3-2"));
		t223.setChildren(t223children);
		t22children.add(t223);

		t2children.add(t22);
		t2children.add(new TableHeader("2-3"));
		list.add(t2);

		List<List<String>> lists = TableHeader.getHeaders(list);

		return lists;
	}
}
